<?php
	require_once('config.php');
	
	function GetNumAlbumSearch($keyword='', $status=null)
	{
		if($keyword == '')
		{
			if($status == null)
				$sql = "SELECT count(album_id) as num FROM `album_tb`
						WHERE `album_status` <= 2";
			else
				$sql = "SELECT count(album_id) as num FROM `album_tb` WHERE `album_status` = $status";
		}					
		else
		{
			if($status == NULL)
				$sql = "SELECT count(album_id) as num FROM `album_tb`
						WHERE 	`album_status` <= 2 AND
								`album_tb`.`album_name` like '%$keyword%'";
			else
				$sql = "SELECT count(album_id) as num FROM `album_tb`
						WHERE 	`album_tb`.`album_name` like '%$keyword%' AND `album_status` = $status";
		}
			
			
		
		$sql = mysql_query($sql, dbconnect());
		$result = mysql_fetch_assoc($sql);
		return $result['num'];
	}
	function GetAlbumTicker()
	{
		$sql = "SELECT * FROM `album_tb`
				WHERE `album_status` = ".NEWS_STATUS_NEW;
					
		$sql = mysql_query($sql, dbconnect());
		
		$result = array(); 
		while ($album_row = mysql_fetch_assoc($sql)) { 
			$result[] = $album_row;
		} 
		return $result; 
	}
	
	function GetAlbumSearch($curPage, $rowPerPage, $keyword='', $status=null)
	{
		if($curPage > 0)
			$curPage--;
		$from = $curPage * $rowPerPage;
		
		
		if($keyword == '')
		{
			if($status == null)	
				$sql = "SELECT *  FROM `album_tb` 
						WHERE `album_status` <= 2
						ORDER BY `album_status` DESC, `album_order` DESC, `album_date` DESC
						LIMIT $from, $rowPerPage ";
			else
				$sql = "SELECT *  FROM `album_tb` 
						WHERE `album_status` = $status
						ORDER BY `album_order` DESC, `album_date` DESC
						LIMIT $from, $rowPerPage ";
		}
		else
		{
			if($status == null)
				$sql = "SELECT *  FROM `album_tb`
						WHERE	`album_status` <= 2 AND
								`album_tb`.`album_name` like '%$keyword%' 
						ORDER BY `album_status` DESC, `album_order` DESC, `album_date` DESC
						LIMIT $from, $rowPerPage";
			else
				$sql = "SELECT *  FROM `album_tb`
						WHERE 	`album_tb`.`album_name` like '%$keyword%' AND		
								`album_status` = $status
						ORDER BY `album_order` DESC, `album_date` DESC
						LIMIT $from, $rowPerPage";
		}
								
		//var_dump($sql); die();
		$sql = mysql_query($sql, dbconnect());
		
		$result = array(); 
		while ($album_row = mysql_fetch_assoc($sql)) { 
			$result[] = $album_row;
		} 
		return $result; 
	}
	function InsertAlbum($arr)
	{
		$arr['album_description'] = addslashes($arr['album_description'] );
		$arr['album_name'] = addslashes($arr['album_name'] );
		
		$sql = "INSERT INTO `album_tb` 
					(	`album_name`,`album_img`,`album_description`) 
				VALUE ('{$arr['album_name']}', '{$arr['url1']}','{$arr['album_description']}' )";
		
		return mysql_query($sql, dbconnect());
	}
	
	function GetAlbumById($id)
	{
		$sql = mysql_query("SELECT * FROM `album_tb`
							WHERE 	`album_tb`.`album_id` = $id", dbconnect());
		$result = mysql_fetch_assoc($sql);
		$result['album_description'] = stripcslashes($result['album_description']);
		$result['album_name'] = stripcslashes($result['album_name']);
		return $result;
		
	}
	function UpdateAlbum($album_id, $arr)
	{
		$arr['album_description'] = addslashes($arr['album_description'] );
		$arr['album_name'] = addslashes($arr['album_name'] );
		
		$sql = "UPDATE `album_tb` 
						SET `album_name`='{$arr['album_name']}', 
							`album_img`   = '{$arr['url1']}',
							`album_description`='{$arr['album_description']}'
							
						WHERE `album_id`=$album_id";
		
		mysql_query($sql, dbconnect());
	}
	function DeleteAlbum($album_id){
		return mysql_query("DELETE FROM `album_tb` WHERE `album_id`= $album_id", dbconnect());
	}
	function UpdateAlbumNew($album_id, $adj)
	{
		if ($adj == 'new')
			$sql = "UPDATE `album_tb` 
							SET `album_status`= 2 
							WHERE `album_id`=$album_id";
		else
			$sql = "UPDATE `album_tb` 
							SET `album_status`= 1
							WHERE `album_id`=$album_id";
		return mysql_query($sql, dbconnect());
	}
	function UpdateAlbumOrder($album_id, $adj)
	{
		if ($adj == '+')
			$sql = "UPDATE `album_tb` 
							SET `album_order`=`album_order` + 1 
							WHERE `album_id`=$album_id";
		else
			$sql = "UPDATE `album_tb` 
							SET `album_order`=`album_order` - 1 
							WHERE `album_id`=$album_id";
		return mysql_query($sql, dbconnect());
	}
	function GetPhotoByAlbumId($album_id, $frontEnd=false)
	{
		if($frontEnd)
			$sql = mysql_query("SELECT * FROM `photo_tb`
								WHERE 	`photo_tb`.`album_id` = $album_id AND 
										`photo_tb`.`photo_status`>0
								ORDER BY `photo_order` DESC", dbconnect());
		else
			$sql = mysql_query("SELECT * FROM `photo_tb`
								WHERE 	`photo_tb`.`album_id` = $album_id 
								ORDER BY `photo_order` DESC", dbconnect());
		$result = array(); 
		while ($product_row = mysql_fetch_assoc($sql)) { 
			$result[] = $product_row;
		} 
		return $result; 
	}
	function DeletePhoto($photo_id){
		return mysql_query("DELETE FROM `photo_tb` WHERE `photo_id`= $photo_id", dbconnect());
	}
	function InsertPhoto($arr)
	{
		$sql = "INSERT INTO `photo_tb` 
							(`album_id`, `photo_url`)
					VALUE	({$arr['album_id']}, '{$arr['photo_url']}')";
		
		return mysql_query($sql, dbconnect());
	}
	function UpdatePhotoOrder($photo_id, $adj)
	{
		if ($adj == '+')
			$sql = "UPDATE `photo_tb` 
							SET `photo_order`=`photo_order` + 1 
							WHERE `photo_id`=$photo_id";
		else
			$sql = "UPDATE `photo_tb` 
							SET `photo_order`=`photo_order` - 1 
							WHERE `photo_id`=$photo_id";
		return mysql_query($sql, dbconnect());
	}
	function GetPhotoByPhotoId($photo_id)
	{
		$sql = mysql_query("SELECT * FROM `photo_tb`
							WHERE 	`photo_tb`.`photo_id` = $photo_id", dbconnect());
		return mysql_fetch_assoc($sql);
	}
?>